package com.etxxy.common.util;

import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @category 此项目使用到的AES加密
 * 
 * @author yiz
 * @date 2016年5月6日 下午2:27:08
 * @version 1.0.0 
 * @copyright pycredit.cn 
 */
public class AESUtil {
	private static final Logger logger = LoggerFactory.getLogger(AESUtil.class);

	private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5PADDING";

	public static final byte[] iv = { 82, 22, 30, 44, -16, 124, -40, -114, -87, -40, 37, 23, -56, 23, -33, 75 };

	public static String encryptPass(String content, String key) {
		byte[] bytes = encrypt(content, key);
		return parseByte2HexStr(bytes);
	}

	public static String decryptPass(String content, String key) {
		byte[] bytes = parseHexStr2Byte(content);
		return new String(decrypt(bytes, key));
	}

	/** 
	 * 加密 .
	 */
	public static byte[] encrypt(String content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
			random.setSeed(password.getBytes());
			kgen.init(128, random);
			AlgorithmParameterSpec parameterSpec = new IvParameterSpec(iv);
			SecretKey secretKey = kgen.generateKey();
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);// 创建密码器  
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);// 初始化  
			byte[] byteContent = content.getBytes("utf-8");
			byte[] result = cipher.doFinal(byteContent);
			return result; // 加密  
		} catch (Exception e) {
			logger.error("加密:{}抛出异常,e={}", content, e);
		}
		return null;
	}

	/**
	 * 解密 
	 */
	public static byte[] decrypt(byte[] content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
			random.setSeed(password.getBytes());
			kgen.init(128, random);
			AlgorithmParameterSpec parameterSpec = new IvParameterSpec(iv);
			SecretKey secretKey = kgen.generateKey();
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);// 创建密码器  
			cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec);// 初始化  
			byte[] result = cipher.doFinal(content);
			return result; // 加密  
		} catch (Exception e) {
			logger.error("解密:{}抛出异常,e={}", content, e);
		}
		return null;
	}

	/**
	 * 将二进制转换成16进制 
	 */
	public static String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (byte element : buf) {
			String hex = Integer.toHexString(element & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 将16进制转换为二进制 
	 */
	public static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1) {
			return null;
		}
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < (hexStr.length() / 2); i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, (i * 2) + 1), 16);
			int low = Integer.parseInt(hexStr.substring((i * 2) + 1, (i * 2) + 2), 16);
			result[i] = (byte) ((high * 16) + low);
		}
		return result;
	}

}
